﻿Public Class CPhieuXuat
    Dim _strMaPhieuXuat As String
    Dim _strMaNhanVien As String
    Dim _strNgayXuat As String
    Property MaPhieuXuat As String
        Get
            Return _strMaPhieuXuat
        End Get
        Set(ByVal value As String)
            _strMaPhieuXuat = value
        End Set
    End Property
    Property MaNhanVien As String
        Get
            Return _strMaNhanVien
        End Get
        Set(ByVal value As String)
            _strMaNhanVien = value
        End Set
    End Property
    Property NgayXuat As String
        Get
            Return _strNgayXuat
        End Get
        Set(ByVal value As String)
            _strNgayXuat = value
        End Set
    End Property
    Shared da As DataProvider
    Public Shared Function LoadDS() As DataTable
        da = New DataProvider()
        Dim sql = "select * from PHIEUXUATKHO"
        Return da.ShowData(sql)
    End Function
    Public Shared Sub Insert(ByVal px As CPhieuXuat, ByVal mh As CMatHang)
        da = New DataProvider
        Dim sql = "insert into PHIEUXUATKHO(NgayXuat,MaNhanVien) values('" + px.NgayXuat + "','" + px.MaNhanVien + "')"
        da.SqlExcuteNonQuery(sql)
        sql = "select MAX(MaPhieuXuat) from PHIEUXUATKHO"
        Dim _strMaPhieuXuat = da.GetData(sql)
        sql = "insert into CTPHIEUXUATKHO values('" + _strMaPhieuXuat + "','" + mh.MaMH + "','" + mh.Loai + "','" + mh.Gia + "','" + mh.XuatXu + "','" + mh.DonVi + "','" + mh.SoLuong + "')"
        da.SqlExcuteNonQuery(sql)
        sql = "select SoLuong from SANPHAM WHERE MaSanPham = '" + mh.MaMH + "'"
        Dim soLuongCu As Integer = da.GetData(sql)
        If soLuongCu = Integer.Parse(mh.SoLuong) Then
            sql = "DELETE FROM SANPHAM WHERE MaSanPham = '" + mh.MaMH + "'"
            da.SqlExcuteNonQuery(sql)
        End If
        Dim soLuongTon As Integer = soLuongCu - Integer.Parse(mh.SoLuong)
        sql = "UPDATE SANPHAM SET SoLuong = '" + soLuongTon.ToString() + "' WHERE MaSanPham = '" + mh.MaMH + "'"

        da.SqlExcuteNonQuery(sql)
    End Sub
    Public Shared Sub Delete(ByVal px As CPhieuXuat)
        da = New DataProvider
        Dim sql = "delete from CTPHIEUXUATKHO where MaPhieuXuat = '" + px.MaPhieuXuat + "'"
        da.SqlExcuteNonQuery(sql)
        sql = "delete from PHIEUXUATKHO where MaPhieuXuat = '" + px.MaPhieuXuat + "'"
        da.SqlExcuteNonQuery(sql)
    End Sub
    Public Shared Sub Update(ByVal px As CPhieuXuat, ByVal mh As CMatHang)
        da = New DataProvider
        Dim sql = "update PHIEUXUATKHO " + _
                  "set NgayXuat = '" + px.NgayXuat + "' and MaNhanVien = '" + px.MaNhanVien + "'" + _
                  "where MaPhieuXuat = '" + px.MaPhieuXuat + "'"
        da.SqlExcuteNonQuery(sql)
        sql = "update CTPHIEUXUATKHO " + _
                  "set MaSanPham = '" + mh.MaMH + "' and Loai = '" + mh.Loai + "' and SoLuong = '" + mh.SoLuong + "'" + _
                  " MaDonVi = '" + mh.DonVi + "' and DonGia = '" + mh.Gia + "'" + _
                  "where MaPhieuXuat = '" + px.MaPhieuXuat + "'"
        da.SqlExcuteNonQuery(sql)
    End Sub
End Class
